
//#define FLASH

#include "prospeckz.h"
#include "prospeckz_radio.h"

#include "defines.h"

#include "dynmem.h"

#include "MAC.h"
#include "xlowpan.h"
#include "StopAndWait.h"
#include "scheduler.h"

struct xlowpan_addr64 MyAddr;
struct xlowpan_addr64 TargetAddr;

unsigned char LED_Colour = 0;

#define DELAY 50000

/* LED COLOURS */
#define RED		6
#define GREEN	3
#define BLUE	5
#define YELLOW	2
#define	MAGENTA	4
#define	CYAN	1
#define	ALL		0
#define	BLANK	7


char string[12];

void testLEDswitch(void)
{
	if(LED_Data_ADDR <= 6)
		LED_Data_ADDR++;
	else
		LED_Data_ADDR = 0;
}

void GotPacket(struct xlowpan_addr64 *from, unsigned char* data, unsigned int length)
{
	int i = DELAY;
	if(length == 0)return;

	LED_Off;
	while(i--);
	LED_GREEN_On;
	i = DELAY;
	while(i--);
	LED_Data_ADDR = data[1];
	
	UART_PutChar('p');
	UART_PutChar('a');
	UART_PutChar('c');
	UART_PutChar('k');
	UART_PutChar('e');
	UART_PutChar('t');
	UART_PutChar(' ');
	UART_PutChar(data[0]+48);
	UART_PutChar(' ');
	UART_PutChar(data[1]+48);
	UART_PutChar('\n');
	
	//UART_CPutString("packet 2 5\n");
	
}

unsigned int SendRadio(struct xlowpan_addr64 *to, void* data, unsigned int length)
{
	return xlowpan_send(to, data,length);
}

unsigned char _memory1[256];
unsigned char _memory2[256];
unsigned char _memory3[256];
unsigned char packet[2];

SCHEDULED_TASK LEDswitchTask;

void main(void)
{
	int pressed = 0;
	int i = 50000;

//	UART_CmdReset(); // Initialize receiver/cmd
	// buffer
//	UART_IntCntl(UART_ENABLE_RX_INT); // Enable RX interrupts
//	UARTCounter_WritePeriod(155); // Set up baud rate generator
//	UARTCounter_WriteCompareValue(77);
//	UARTCounter_Start(); // Turn on baud rate generator
//	UART_Start(UART_PARITY_NONE); // Enable UART
	LEDswitchTask.task.execute = &testLEDswitch;
	LEDswitchTask.period = 32736;
	LEDswitchTask.basePeriod = CLOCK_PERIODS;
	
	
	mainTimer_EnableInt();
	M8C_EnableGInt;

	
//	DEBUGLOGC("Debug port\r\n");
/*	
	MyAddr.addr[0] = 'a';
	MyAddr.addr[1] = 'w';
	MyAddr.addr[2] = 'e';
	MyAddr.addr[3] = 's';
	MyAddr.addr[4] = 'o';
	MyAddr.addr[5] = 'm';
	MyAddr.addr[6] = 'e';
	MyAddr.addr[7] = 2;
	
	TargetAddr.addr[0] = 'a';
	TargetAddr.addr[1] = 'w';
	TargetAddr.addr[2] = 'e';
	TargetAddr.addr[3] = 's';
	TargetAddr.addr[4] = 'o';
	TargetAddr.addr[5] = 'm';
	TargetAddr.addr[6] = 'e';
	TargetAddr.addr[7] = 1;

	dynmem_init(_memory1, 256);
	dynmem_append(_memory2, 256);
	dynmem_append(_memory3, 256);
*/
	// Turn off LED for power...
		
	LED_Data_ADDR = RED;
	while(i--);
	//M8C_EnableGInt;
	//M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO);	//Enable interupts on the GPIO port
//	Reliable_Init(&MyAddr);

	//Reliable_SetReceiveHandler(&GotPacket);
	//Reliable_SetSendHandler(&SendRadio);
//	while(1)
//	{
//		MAC_CheckRadio();
//	}
	
	sched_init();
	mainTimer_Start();
	sched_addTimedTask(&LEDswitchTask);
    sched_mainLoop();
}

#pragma interrupt_handler PSoC_GPIO_ISR
void PSoC_GPIO_ISR(void)
{
	M8C_DisableGInt;
	//while(MAC_CheckRadio());
	M8C_EnableGInt;
}

#pragma interrupt_handler mainTimer_ISR
void mainTimer_ISR(void)
{
	extern volatile int currentTime;
	extern volatile int nextmsInterrupt;
	extern volatile int nextSecInterrupt;
	extern boolean secDelayTimerActive;
	extern boolean msDelayTimerActive;
//	LED_Data_ADDR = ALL;
	//currentTime = mainTimer_wReadCounter();
	currentTime += MILISECONDS_INCREMENT;
	if(currentTime >= SECONDS_INCREMENT) 
	{	
		currentTime = 0;
	}
	if(msDelayTimerActive)
	{
		if(currentTime >= nextmsInterrupt)
		{
			sched_taskTimerISR(0);
		}
		//1 ms tick, should call scheduler and check timedtasks based on SECOND time_period
	//	return;
	}
	if(secDelayTimerActive) //secDelayTimerActive)
	{
		
		if(currentTime >= nextSecInterrupt)
		{
			//testLEDswitch();
			sched_taskTimerISR(1);
		}
		//1 sec. tick, should call scheduler and check timedtasks based on SECOND time_period
	//	return;
	}

}